package pzc.context.util;

import javax.naming.Context;
import javax.naming.NameClassPair;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;

public class ContextViewer {

	public static void showAll(Context context) throws NamingException {
		showAll(context, 0);
	}

	private static void showAll(Context context, int step) throws NamingException {
		NamingEnumeration<NameClassPair> ne = context.list("");
		while (ne.hasMore()) {
			NameClassPair e = ne.next();
			System.out.println(makeSpace(step) + e);
			Object o = context.lookup(e.getName());
			if (o instanceof Context) {
				showAll((Context) o, step + 1);
			}
		}
	}

	private static String makeSpace(int step) {
		String ret = "";
		for (int i = 0; i < step; i++) {
			ret += "  ";
		}
		return ret;
	}
}
